/**
 * 预支账户报销单
 */
/**
 * 表单自定义控制器
 */
app.controller('btnCtrl', ['$scope', '$http', BtnController]);
function BtnController($scope, $http) {
	$scope.bindBtn = function(event) {
		switch(event){
			case 'event-to-add': // 新增
			{
				var code =  $scope.valueMap["code"];
				var advanceAccountId = $scope.valueMap["advanceAccountId"];//预支账户管理表id
				if(code==undefined || code==null || code==''){
					layer.msg("请先选择预支账户编码!");
					return;
				}
				var companyVid = $scope.valueMap["companyVid"];//所属公司编码
				if(companyVid==undefined || companyVid==null || companyVid==''){
					layer.msg("请先选择所属公司!");
					return;
				}
				var listUkey = [];
				for(var i=0;i<$scope.subGrid.sonlist.length;i++){
					var data = $scope.subGrid.sonlist[i];
					listUkey.push(data.id);
				}
				$scope.showModalCheckBox('TCW_AdvanceAccountDetail', 'id', listUkey,[{code: 'pid', value: advanceAccountId},{code: 'companyInfoVid', value: companyVid}]);
				//layui.YsModal.init({modalKey: 'TCW_AdvanceAccountDetail', type: 'checkbox', uniqueKey: 'id', parentObj: t.c.subGridObj, selectedData: selectedDatas, extraData:{}, conditionsDefault: [{code: 'pid', value: advanceAccountId},{code: 'companyInfoVid', value: companyVid}]});

				break;
			}
			case 'event-to-delete': // 删除选中数据
			{
				$scope.subGrid.deleteBatch();

				//计算本次报销金额
				var totalPriceByThis = 0;
				if($scope.subGrid.sonlist!=null && $scope.subGrid.sonlist.length>0){
					for(var i=0;i<$scope.subGrid.sonlist.length;i++){
						var totalPriceByItem = parseFloat($scope.subGrid.sonlist[i].totalPrice);
						if(totalPriceByItem){
							totalPriceByThis = totalPriceByThis+totalPriceByItem;
						}
					}
				}
				$scope.valueMap["totalPriceByThis"] = totalPriceByThis;
				break;
			}
		};
	}

}





/*
* 子列表控制器
*/
app.controller('subGridCtrl', ['$scope', '$http', function SubGridController($scope, $http){
	/*
	 * 复选框回调
	 */
	$scope.checkbox.afterSelectDatas = function(rowsSelected) {
		console.log('formSelfCtrl2---', rowsSelected);
		var rows = [];
		for (var i = 0; i < rowsSelected.length; i++) {
			var rowSelected = rowsSelected[i];

			var isAdd = true; // 根据现有存在，是否加新行
			for (var j = 0; j < $scope.subGrid.sonlist.length; j++) {
				if ($scope.subGrid.sonlist[j].advanceAccountDetailId == rowSelected.id) {
					isAdd = false;
				}
			}

			if (isAdd == true) {
				var row = {
					reimbursementItemId: null,
					advanceAccountDetailId: rowSelected.id,
					id:rowSelected.id,
					createTime:rowSelected.createTime,
					companyName:rowSelected.companyName,
					advanceTypeName:rowSelected.advanceTypeName,
					vjylx:rowSelected.vjylx,
					detailName:rowSelected.detailName,
					unitPrice:rowSelected.unitPrice,
					quantity:rowSelected.quantity,
					totalPrice:rowSelected.totalPrice,
					remark:rowSelected.remark,
					userName:rowSelected.userName,
					deptName:rowSelected.deptName,
					annexFile:rowSelected.annexFile,
					detailType:rowSelected.detailType,
					vgsbm:rowSelected.vgsbm,
					proxyPrice:rowSelected.proxyPrice,
					proxyTotalPrice:rowSelected.proxyTotalPrice,
				};
				$scope.subGrid.sonlist.push(row);
			}
		}


		//计算本次报销金额
		var totalPriceByThis = 0;
		if($scope.subGrid.sonlist!=null && $scope.subGrid.sonlist.length>0){
			for(var i=0;i<$scope.subGrid.sonlist.length;i++){
				var totalPriceByItem = parseFloat($scope.subGrid.sonlist[i].totalPrice);
				if(totalPriceByItem){
					totalPriceByThis = totalPriceByThis+totalPriceByItem;
				}
			}
		}
		$scope.valueMap["totalPriceByThis"] = totalPriceByThis;
	}
}]);
app.controller('formSelfCtrl', ['$scope', '$http', FormSelfController]);
function FormSelfController($scope, $http){

	//监听子表数字输入框变更
	$scope.subGrid.numberChange = function(row, field) {
		if (field == 'amount') {

			//计算总金额
			var amount = 0 ;
			if($scope.subGrid.sonlist!=null && $scope.subGrid.sonlist.length>0){
				for(var i=0;i<$scope.subGrid.sonlist.length;i++){
					if($scope.subGrid.sonlist[i].amount){
						amount = amount+parseFloat($scope.subGrid.sonlist[i].amount);
					}
				}
			}

			//回填
			$scope.valueMap['totalAmount'] = amount.toFixed(2);
		}
	}


}


